package com.juyu.miliao.scheduler.handler;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.juyu.miliao.scheduler.service.VipExpireService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.concurrent.*;


/**
 * vip过期处理器
 *
 * @author xuanqicheng
 */
@Component
public class VipExpireHandler implements CommandLineRunner {

    private Logger logger = LoggerFactory.getLogger(VipExpireHandler.class);

    @Autowired
    private VipExpireService vipExpireService;

    /**
     * 固定大小线程池
     */
    private ExecutorService executor;


    /**
     * 初始化
     */
    public void init() {
        ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("线程[VipExpireHandler]-%s").build();
        executor = new ThreadPoolExecutor(1, 1, 60L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(), factory);
        executor.execute(new VipExpireQueueThread(vipExpireService));

    }

    @Override
    public void run(String... args) throws Exception {
        init();
        logger.info("初始化：{}",VipExpireHandler.class);
    }
}
